package com.heima.test8;

import java.util.concurrent.*;

public class Demo03 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //1 创建线程池对象
        ThreadPoolExecutor pool = new ThreadPoolExecutor(2, // 核心线程和数量
                5,//  全部的线程最大值
                5, TimeUnit.SECONDS, //空闲线程的存活时间和单位
                new ArrayBlockingQueue<>(3),// 任务的队列
                Executors.defaultThreadFactory(),//创建线程工厂
                new ThreadPoolExecutor.AbortPolicy() //任务的拒绝策略
        );

        //2 执行任务 参数接收一个Runnable对象
        Future<String> fa = pool.submit(new MyCallable("aa"));
        Future<String> fb = pool.submit(new MyCallable("bb"));
        Future<String> fc = pool.submit(new MyCallable("cc"));
        Future<String> fd = pool.submit(new MyCallable("dd"));
        Future<String> fe = pool.submit(new MyCallable("ee"));
        Future<String> ff = pool.submit(new MyCallable("ff"));

        System.out.println(fa.get());
        System.out.println(fb.get());
        System.out.println(fc.get());
        System.out.println(fd.get());
        System.out.println(fe.get());
        System.out.println(ff.get());


        //3 shutdown 可以让线程池在执行完任务后 结束
        pool.shutdown();

        System.out.println("111111111111111");

    }
}